require("../../assets/css/index.less")
require('weui')
require("./changeInfo.less")

const weui = require("weui.js")
const { getEle, isLogin, ready, addEvent } = require("../../utils/dom")
const { get, post, BASE_URL } = require('../../utils/https')

isLogin()

let proId = null;
let cityId = null;
ready(() => {
    addEvent("#goBack", () => history.back())


    function getInfo() {
        get("/api/user/info", res => {
            let { errno, data } = res
            if (errno == 0) {
                getEle("#imgUrl").src = BASE_URL + data.imgUrl
                getEle("#nickName").value = data.nickName
                if (data.gender || data.gender == 0) {
                    getEle("#sexText").textContent = data.gender == 0 ? "男" : "女"
                }
                if (data.birthday) {
                    getEle("#birthday").textContent = data.birthday
                }
                if (data.provinceId) {
                    proId = data.provinceId;
                    get("/api/shared/province", res => {
                        let obj = res.data.find(item => item.id == data.provinceId)
                        getEle("#province").textContent = obj.name
                    })
                }
                if (data.cityId) {
                    get(`/api/shared/city?provinceId=${proId}`, res => {
                        let obj = res.data.find(item => item.id == data.cityId)
                        getEle("#city").textContent = obj.name;
                    })
                }
                if (data.sign) {
                    getEle("#signText").textContent = data.sign
                    getEle("#curNum").textContent = data.sign.length
                }
            } else {
                weui.alert("请先登录", () => location.href = "login.html")
            }
        })
    }
    getInfo()

    function getSex() {
        weui.picker([{
            label: '男',
            value: 0
        }, {
            label: '女',
            value: 1
        }], {
            //确定的时候触发
            onConfirm: result => getEle("#sexText").textContent = result[0].label,
            title: '请选择性别'
        });
    }
    addEvent("#sex", () => getSex())

    function getBirthday() {
        weui.datePicker({
            start: new Date().getFullYear() - 80,
            end: new Date().getFullYear(),
            onConfirm: result => getEle('#birthday').textContent = result.map(item => item.value <= 9 ? '0' + item.value : item.value).join('-'),
            title: '请选择出生日期'
        });
    }
    addEvent("#birthdayText", () => getBirthday())

    function getProvince() {
        get("/api/shared/province", (res) => {
            let { errno, data } = res
            if (errno == 0) {
                data = data.map((item) => {
                    return {
                        label: item.name,
                        value: item.id
                    }
                })
                weui.picker(data, {
                    onConfirm: (result) => {
                        getEle('#province').textContent = result[0].label
                        proId = result[0].value
                        cityId = null;
                        getCity()
                    },
                    title: '请选择所在省份'
                });
            }else{
                weui.alert(res.message)
            }
        })
    }
    addEvent("#getPro", () => getProvince())

    function getCity() {
        if (proId) {
            get(`/api/shared/city?provinceId=${proId}`, res => {
                let { errno, data } = res
                if (errno == 0) {
                    data = data.map(function (item) {
                        return {
                            label: item.name,
                            value: item.id
                        }
                    })
                    weui.picker(data, {
                        onConfirm: function (result) {
                            getEle('#city').textContent = result[0].label
                            cityId = result[0].value
                        },
                        title: '请选择所在城市'
                    });
                }
            })
        } else {
            getProvince()
        }
    }
    addEvent("#getCity", () => getCity())

    function saveInofo() {
        let data = {
            nickName: getEle("#nickName").value,
            gender: getEle("#sexText").textContent == "男" ? 0 : 1,
            provinceId: proId,
            cityId: cityId,
            birthday: getEle("#birthdayText").textContent,
            imgUrl: getEle("#imgUrl").src.replace(BASE_URL, ""),
            sign: getEle("#signText").value,
        }
        post("/api/user/changeInfo", data, res => {
            if (res.errno == 0) {
                weui.toast("修改成功", function () {
                    location.href = "my.html"
                })
            } else {
                weui.alert("修改失败")
            }
        })
    }
    addEvent("#saveBtn", () => saveInofo())

    getEle("#file").addEventListener("change", function () {
        let file = this.files[0];
        let fd = new FormData();
        fd.append("file", file);
        post("/api/shared/uploadPortrait", fd, res => {
            let { errno, data } = res
            if (errno == 0) {
                getEle("#imgUrl").src = BASE_URL + data.url
            } else {
                weui.alert("修改失败")
            }
        })
    })

    addEvent("#signText", () => {
        getEle("#curNum").textContent = getEle("#signText").value.length
    }, 'input')

    addEvent("#imgUrl", () => getEle("#file").click())








})